﻿using System;
using System.Windows;
using Brjnk.Common;
using Brjnk.GpxExplorer.ViewModel;

namespace Brjnk.GpxExplorer.Computing
{
    public class FindTimeResult : FindResult
    {
        public GpsCoordinates Position { get; protected set; }

        public Distance? Elevation { get; protected set; }

        public FindTimeResult(IPoint previousPoint, IPoint nextPoint, double p)
        {
            if (p < 0 && p > 1) throw new ArgumentOutOfRangeException("parametr"); //parametr úsečky musí být od 0 do 1
            this.PreviousPoint = previousPoint;
            this.NextPoint = nextPoint;
            this.Parameter = p;
            this.Position = (GpsCoordinates)LineSegmentHelper.GetPointByParameter((Point)previousPoint.PositionI, (Point)nextPoint.PositionI, p);

            var previousTrkPoint = previousPoint as TrackPointVM;
            var nextTrkPoint = nextPoint as TrackPointVM;
            if (previousTrkPoint != null &&
                nextTrkPoint != null &&
                previousTrkPoint.Source.Elevation.HasValue &&
                nextTrkPoint.Source.Elevation.HasValue)
                this.Elevation = previousTrkPoint.Source.Elevation + (previousTrkPoint.Source.Elevation - nextTrkPoint.Source.Elevation) * p;
        }
    }
}